﻿using System;

namespace Wombat.Core.Ports
{
    public class GenericObserverPort<T>: IObserverPort<T> where T: ITagValue
    {
        private readonly Action<T> _onNext;
        private readonly Action<Exception> _onError;
        private readonly Action _onCompleted;
        public string Name { get; private set; }
        public Guid Guid { get; private set; }
        public PortBehavior PortBehavior { get; private set; }

        internal GenericObserverPort(string name, Guid id, PortBehavior portBehavior, Action<T> onNext, Action<Exception> onError, Action onCompleted)
        {
            Name = name;
            Guid = id;
            PortBehavior = portBehavior;
            _onNext = onNext;
            _onError = onError;
            _onCompleted = onCompleted;
        }

        public void OnNext(T value)
        {
            if (_onNext == null)
                throw new InvalidOperationException("OnNext is not defined for the observer.");
            _onNext(value);
        }

        public void OnError(Exception error)
        {
            if (_onError == null)
                throw new InvalidOperationException("OnError is not defined for the observer.");
            _onError(error);
        }

        public void OnCompleted()
        {
            if (_onCompleted == null)
                throw new InvalidOperationException("OnCompleted is not defined for the observer.");
            _onCompleted();
        }
    }
}
